package map.android.baidu.carpool.page.pay;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.baidu.components.api.extra.APIProxy;
import map.android.baidu.carpool.R;
import map.android.baidu.carpool.page.CarpoolBasePage;
import map.android.baidu.carpool.page.home.CarpoolHomePage;
import map.android.baidu.carpool.response.CreateCommentResponse;
import map.android.baidu.carpool.utils.OrderHongbaoShareDialog;
import map.android.baidu.carpool.utils.StatisticsUtil;
import map.android.baidu.carpool.utils.TitleHelpler;

/**
 * Created by zhangtongyu on 15/4/7.
 */
public class CommentSuccessPage extends CarpoolBasePage{

    private Context mContext;
    private Resources mRes;
    private LayoutInflater mInflater;

    private View mView;
    private TitleHelpler mTitleHelper;

    Button mBottomBt;

    private Bundle mBundle;

    private TextView mShareNotice;
    private LinearLayout mCommentSuccessTitleLin;
    private View mCommentSuccessDivide;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mContext = APIProxy.res().getBaseActivity();
        mRes = APIProxy.res().getResources();
        mInflater = APIProxy.res().getInflater();

        if (mView == null) {
            mBundle = getArguments();
            mView = APIProxy.res().inflate(R.layout.layout_page_comment_success);
            initView();
        } else {
            ViewGroup parent = (ViewGroup) mView.getParent();
            if (parent != null) {
                parent.removeView(mView);
            }
        }
        return mView;
    }

    private void initView() {
        mBundle = getArguments();

        mTitleHelper = new TitleHelpler(mView);
        mTitleHelper.setCenterView("评价成功");
        mTitleHelper.setLeftView(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                APIProxy.navi().navigateTo(CarpoolHomePage.class, null, true);
            }
        });
        mCommentSuccessTitleLin = (LinearLayout) mView.findViewById(R.id.lin_share_title_container);
        mCommentSuccessDivide = mView.findViewById(R.id.v_comment_success_divide);
        mShareNotice = (TextView) mView.findViewById(R.id.tv_comment_succes_notice);
        mBottomBt = (Button) mView.findViewById(R.id.bt_back_home);
        if(mBundle != null) {
            if(mBundle.containsKey(CreateCommentResponse.class.getName())) {
                CreateCommentResponse createCommentResponse = (CreateCommentResponse) mBundle.getSerializable(CreateCommentResponse.class.getName());
                if(createCommentResponse != null && createCommentResponse.data != null && createCommentResponse.data.shareInfo != null){
                    mCommentSuccessDivide.setVisibility(View.VISIBLE);
                    mCommentSuccessTitleLin.setVisibility(View.VISIBLE);
                    mShareNotice.setText(createCommentResponse.data.shareInfo.title);
                }
            }
        }
        CreateCommentResponse createCommentResponse = (CreateCommentResponse) mBundle.getSerializable(CreateCommentResponse.class.getName());
        if(createCommentResponse != null && createCommentResponse.data != null && createCommentResponse.data.shareInfo != null){
        mBottomBt.setText("分享行程");
        mBottomBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StatisticsUtil.getInstance().addStatisticsOffline(StatisticsUtil.OFFLINE_ACT_COMMENT_SUCCESS_PAGE, StatisticsUtil.OFFLINE_ACT_SHARE_BT);
                if (mBundle.containsKey(CreateCommentResponse.class.getName())) {
                    CreateCommentResponse createCommentResponse = (CreateCommentResponse) mBundle.getSerializable(CreateCommentResponse.class.getName());
                    if (createCommentResponse != null && createCommentResponse.data != null) {
                        OrderHongbaoShareDialog dialog = new OrderHongbaoShareDialog(getActivity(), createCommentResponse);
                        dialog.show();
                        mBottomBt.setText("再订一单");
                        mBottomBt.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                APIProxy.navi().navigateTo(CarpoolHomePage.class, null, true);
                            }
                        });
                    }
                }
            }
        });
        } else {
            mBottomBt.setText("再订一单");
            mBottomBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    APIProxy.navi().navigateTo(CarpoolHomePage.class, null, true);
                }
            });
        }
    }

    @Override
    public boolean onBackPressed() {
        APIProxy.navi().navigateTo(CarpoolHomePage.class, null, true);
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

}
